package com.hewanning.toy.play.controller;

import com.hewanning.toy.play.model.dto.UserDto;
import com.hewanning.toy.play.model.entity.User;
import com.hewanning.toy.play.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户接口
 *
 * @author robin_he
 * @date 2020/1/3
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public Object getList() {
        List<User> list = userService.list();
        return list;
    }

    @PostMapping
    public Object saveUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    @DeleteMapping("/{id}")
    public Object deleteUser(@PathVariable("id") Integer id) {
        return userService.removeById(id);
    }

    @PostMapping(value = "/insert", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Object insertUser(UserDto dto) {
        User user = new User();
        user.setName(dto.getName());
        user.setEmail(dto.getEmail());
        user.setPassword(dto.getContent());
        userService.save(user);
        return "success";
    }
}
